ConfigManager = ConfigManager or Class("ConfigManager", EventDispatcher)
ConfigEvent = RegisterEvents{
    "AddSelection",
    "AddText",
    "DeleteText",
    "AddLanguage",
    "DeleteSelection",
    "ChangeSelectionName",
    "NewSelectionElement",
    "ChangeSelectionElementName",
    "ChangeSelectionElementDesc",
    "ChangeLanguageName",
    "ChangeLanguageDesc",
    "Reload",
    "ChangeTextName",
    "AddTable"
}
---@diagnostic disable-next-line: duplicate-set-field
function ConfigManager:ctor()
    EventDispatcher.ctor(self)
    self._configSeletionIdCreator = IdCreator.new()
    self._selections = {}
    self._selectionsDic = {}
    self._configTables = {}
    self._tableDic = {}
    self._configTableIdCreator = IdCreator.new()

    self._textIdCreator = IdCreator.new()
    self._textList = {}
    self._textDic = {}
    self._languageIdCreator = IdCreator.new(2)
    self._languages = {}
    self._languageDic = {}
    self:loadLanguage{
        id = 1,
        name = "简体中文",
        desc = "默认"
    }
end

-- function ConfigManager:createConfigSelection(keys)
--     local tbl = {}
--     local selection = ConfigSelection.new()
--     tbl.id = selection.id
--     for _, key in ipairs(keys) do
--         tbl[key] = selection:addElements()
--     end
--     table.insert(self._selections, selection)
--     self._selectionsDic[selection.id] = selection
--     return tbl
-- end

-- 编辑器操作添加选项
function ConfigManager:addSelection()
    local selection = ConfigSelection.new({
        id = self._configSeletionIdCreator:create(),
        name = "选项"..self._configSeletionIdCreator:getCount(),
        desc = "无"
    })
    selection:newElement()
    table.insert(self._selections, selection)
    self._selectionsDic[selection.id] = selection
    self:emit(ConfigEvent.AddSelection)
    return selection
end

function ConfigManager:addText()
    local text = {
        id = self._textIdCreator:create(),
        name = "文本"..self._textIdCreator:getCount(),
        desc = "无"
    }
    self:loadText(text)
    self:emit(ConfigEvent.AddText)
    return text
end

function ConfigManager:loadText(args)
    local text = ConfigText.new(args)
    table.insert(self._textList, text)
    self._textDic[text.id] = text
    self._textIdCreator:load(text.id)
end

function ConfigManager:addLanguage()
    local language = ConfigTextLanguage.new({
        id = self._languageIdCreator:create(),
        name = "语言"..self._languageIdCreator:getCount(),
        desc = "无"
    })
    self:loadLanguage(language)
    self:emit(ConfigEvent.AddLanguage)
end

function ConfigManager:loadLanguage(args)
    local language = ConfigTextLanguage.new(args)
    table.insert(self._languages, language)
    self._languageDic[language.id] = language
    self._languageIdCreator:load(language.id)
end

-- 读档加载选项
function ConfigManager:loadSelection(data)
    local selection = ConfigSelection.new(data)
    table.insert(self._selections, selection)
    self._selectionsDic[selection.id] = selection
    self._configSeletionIdCreator:load(selection.id)
    return selection
end

function ConfigManager:deleteSelection(id)
    if self._selectionsDic[id] then
        local selection = self._selectionsDic[id]
        self._selectionsDic[id] = nil
        for i = #self._selections, 1, -1 do
            if self._selections[i] == selection then
                table.remove(self._selections, i)
                break
            end
        end
        self:emit(ConfigEvent.DeleteSelection)
    end
end
function ConfigManager:getLanguages()
    return self._languages
end
function ConfigManager:getSelections()
    return self._selections
end

function ConfigManager:getTables()
    return self._configTables
end

function ConfigManager:getTexts()
    return self._textList
end

function ConfigManager:getText(id)
    return self._textDic[id]
end

function ConfigManager:getTextAllLanguages(id)
    local texts = {}
    for _, language in ipairs(self._languages) do
        local text = self._textDic[id]:getText(language.id)
        table.insert(texts, text)
    end
    return texts
end

function ConfigManager:getSelection(id)
    if not self._selectionsDic[id] then
        Console.error("ConfigManager:getSelection id not exist", id)
    end
    return self._selectionsDic[id]
end

function ConfigManager:getSelectionCount()
    return #self._selections
end

function ConfigManager:addTable()
    local tbl = self:loadTable({id = self._configTableIdCreator:create()})
    self:emit(ConfigEvent.AddTable)
    return tbl
end

function ConfigManager:loadTable(args)
    local tbl = ConfigTable.new(args)
    table.insert(self._configTables, tbl)
    self._tableDic[tbl.id] = tbl
    return tbl
end

function ConfigManager:getConfigTable(id)
    if not self._configTables[id] then
        Console.error("ConfigManager:getConfigTable id not exist", id)
    end
    return self._configTables[id]
end

function ConfigManager:getSaveData()
    local data = {}
    local selections = {}
    data.selections = selections
    for i, s in ipairs(self._selections) do
        table.insert(selections, s:getSaveData())
    end
    return data
end

function ConfigManager:clear()
    self._configSeletionIdCreator:load(1)
    self._selections = {}
    self._selectionsDic = {}
    self._configTables = {}
    self._configTableIdCreator:load(1)
end

function ConfigManager:fromJson(json)
    self:clear()
    for i, s in ipairs(json.selections) do
        self:loadSelection(s)
    end
    self:emit(ConfigEvent.Reload)
end